home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / pavt150.zip / CAPI.ZIP / JAMFETCH.C < prev    next >
C/C++ Source or Header  |  1993-07-01  |  6KB  |  212 lines

  1. /*
  2. **  JAM(mbp) - The Joaquim-Andrew-Mats Message Base Proposal
  3. **
  4. **  C API
  5. **
  6. **  Written by Joaquim Homrighausen.
  7. **
  8. **  ----------------------------------------------------------------------
  9. **
  10. **  jamfetch.c (JAMmb)
  11. **
  12. **  Fetch messages and texts
  13. **
  14. **  Copyright 1993 Joaquim Homrighausen, Andrew Milner, Mats Birch, and
  15. **  Mats Wallin. ALL RIGHTS RESERVED.
  16. **
  17. **  93-06-28    JoHo
  18. **  Initial coding. No decryption or unescaping supported yet.
  19. */
  20. #define JAMCAPI 1
  21.  
  22. #include <string.h>
  23. #include "jammb.h"
  24.  
  25. /*
  26. **  Fetch specified message number. Returns 1 upon success and 0 upon
  27. **  failure.
  28. */
  29. int _JAMPROC JAMmbFetchMsgHdr(JAMAPIRECptr apirec, UINT32 WhatMsg, int WithSubFields)
  30. {
  31.     INT32 ReadCount;
  32.  
  33.     /* Fetch index record, checks for IsOpen */
  34.     if (!JAMmbFetchMsgIdx(apirec, WhatMsg))
  35.         return (0);
  36.  
  37.     /* Fetch header */
  38.     if (apirec->SeekFunc(apirec, apirec->HdrHandle, JAMSEEK_SET, (INT32)apirec->Idx.HdrOffset)!=(INT32)apirec->Idx.HdrOffset)
  39.         {
  40.         apirec->APImsg=JAMAPIMSG_SEEKERROR;
  41.         return (0);
  42.         }
  43.     if (apirec->ReadFunc(apirec, apirec->HdrHandle, &apirec->Hdr, (INT32)sizeof(JAMHDR))!=(INT32)sizeof(JAMHDR))
  44.         {
  45.         apirec->APImsg=JAMAPIMSG_CANTRDFILE;
  46.         return (0);
  47.         }
  48.  
  49.     /* Check header */
  50.     if (strcmp(apirec->Hdr.Signature, HEADERSIGNATURE)!=0)
  51.         {
  52.         apirec->APImsg=JAMAPIMSG_BADHEADERSIG;
  53.         return (0);
  54.         }
  55.     if (apirec->Hdr.Revision!=CURRENTREVLEV)
  56.         {
  57.         apirec->APImsg=JAMAPIMSG_BADHEADERREV;
  58.         return (0);
  59.         }
  60.  
  61.     /* Fetch subfields if told to */
  62.     if (WithSubFields)
  63.         {
  64.         if (apirec->Hdr.SubfieldLen>apirec->WorkLen)
  65.             ReadCount=(INT32)apirec->WorkLen;
  66.         else
  67.             ReadCount=(INT32)apirec->Hdr.SubfieldLen;
  68.         }
  69.     else
  70.         ReadCount=0L;
  71.  
  72.     if (ReadCount)
  73.         {
  74.         if (apirec->ReadFunc(apirec, apirec->HdrHandle, apirec->WorkBuf, ReadCount)!=ReadCount)
  75.             {
  76.             apirec->APImsg=JAMAPIMSG_CANTRDFILE;
  77.             return (0);
  78.             }
  79.         }
  80.  
  81.     /* Got it OK */
  82.     apirec->APImsg=JAMAPIMSG_NOTHING;
  83.     return (1);
  84. }
  85.  
  86. /*
  87. **  Fetch index record with specified message number. Returns 1 upon success
  88. **  and 0 upon failure.
  89. */
  90. int _JAMPROC JAMmbFetchMsgIdx(JAMAPIRECptr apirec, UINT32 WhatMsg)
  91. {
  92.     INT32 WhatOffset;
  93.  
  94.     /* Make sure it's open */
  95.     if (!apirec->isOpen)
  96.         {
  97.         apirec->APImsg=JAMAPIMSG_ISNOTOPEN;
  98.         return (0);
  99.         }
  100.  
  101.     /* Make sure the message number is valid */
  102.     if (WhatMsg<apirec->HdrInfo.BaseMsgNum)
  103.         {
  104.         apirec->APImsg=JAMAPIMSG_INVMSGNUM;
  105.         return (0);
  106.         }
  107.  
  108.     /* Fetch index record */
  109.     WhatOffset=(INT32)((WhatMsg-apirec->HdrInfo.BaseMsgNum) * (INT32)sizeof(JAMIDXREC));
  110.     if (apirec->SeekFunc(apirec, apirec->IdxHandle, JAMSEEK_SET, WhatOffset)!=WhatOffset)
  111.         {
  112.         apirec->APImsg=JAMAPIMSG_SEEKERROR;
  113.         return (0);
  114.         }
  115.     if (apirec->ReadFunc(apirec, apirec->IdxHandle, &apirec->Idx, (INT32)sizeof(JAMIDXREC))!=(INT32)sizeof(JAMIDXREC))
  116.         {
  117.         apirec->APImsg=JAMAPIMSG_CANTRDFILE;
  118.         return (0);
  119.         }
  120.  
  121.     /* Update structure, even if below fails */
  122.     apirec->LastMsgNum=WhatMsg;
  123.  
  124.     /* Got it OK */
  125.     apirec->APImsg=JAMAPIMSG_NOTHING;
  126.     return (1);
  127. }
  128.  
  129. /*
  130. **  Fetch message following APIREC->LastMsgNum. Returns 1 upon success and
  131. **  0 upon failure.
  132. */
  133. int _JAMPROC JAMmbFetchNextMsgHdr(JAMAPIRECptr apirec, int WithSubFields)
  134. {
  135.     return (JAMmbFetchMsgHdr(apirec, (apirec->LastMsgNum+1L), WithSubFields));
  136. }
  137.  
  138. /*
  139. **  Fetch message preceding APIREC->LastMsgNum. Returns 1 upon success and
  140. **  0 upon failure.
  141. */
  142. int _JAMPROC JAMmbFetchPrevMsgHdr(JAMAPIRECptr apirec, int WithSubFields)
  143. {
  144.     if (apirec->LastMsgNum)
  145.         {
  146.         return (JAMmbFetchMsgHdr(apirec, (apirec->LastMsgNum-1L), WithSubFields));
  147.         }
  148.     else
  149.         {
  150.         apirec->APImsg=JAMAPIMSG_FIRSTMSG;
  151.         return (0);
  152.         }
  153. }
  154.  
  155. /*
  156. **  Fetch text for specified message number. Returns 1 upon success and
  157. **  0 upon failure. FirstFetch determines if the function seeks to the
  158. **  actual text position or simply keeps reading.
  159. */
  160. int _JAMPROC JAMmbFetchMsgTxt(JAMAPIRECptr apirec, int FirstFetch)
  161. {
  162.     INT32 RemainToRead, ReadCount;
  163.  
  164.     /* Make sure it's open */
  165.     if (!apirec->isOpen)
  166.         {
  167.         apirec->APImsg=JAMAPIMSG_ISNOTOPEN;
  168.         return (0);
  169.         }
  170.  
  171.     /* Seek to appropriate text position if this is first fetch */
  172.     if (FirstFetch)
  173.         {
  174.         if (apirec->SeekFunc(apirec, apirec->TxtHandle, JAMSEEK_SET, (INT32)apirec->Hdr.TxtOffset)!=(INT32)apirec->Hdr.TxtOffset)
  175.             {
  176.             apirec->APImsg=JAMAPIMSG_SEEKERROR;
  177.             return (0);
  178.             }
  179.         else
  180.             /* Haven't read anything yet */
  181.             apirec->WorkPos=0L;
  182.         }
  183.  
  184.     /* Make sure we don't read more than we have */
  185.     if (apirec->WorkPos>=apirec->Hdr.TxtLen)
  186.         {
  187.         apirec->APImsg=JAMAPIMSG_NOMORETEXT;
  188.         return (1);
  189.         }
  190.  
  191.     /* Figure out how much to get */
  192.     RemainToRead=(INT32)(apirec->Hdr.TxtLen-apirec->WorkPos);
  193.     if ((INT32)RemainToRead>(INT32)apirec->WorkLen)
  194.         ReadCount=apirec->WorkLen;
  195.     else
  196.         ReadCount=RemainToRead;
  197.  
  198.     /* Get it from disk */
  199.     if (apirec->ReadFunc(apirec, apirec->TxtHandle, apirec->WorkBuf, ReadCount)!=ReadCount)
  200.         {
  201.         apirec->APImsg=JAMAPIMSG_CANTRDFILE;
  202.         return (0);
  203.         }
  204.  
  205.     /* Update how much data we've read */
  206.     apirec->WorkPos+=(UINT32)ReadCount;
  207.     apirec->APImsg=JAMAPIMSG_NOTHING;
  208.     return (1);
  209. }
  210.  
  211. /* end of file "jamfetch.c" */
  212.